package com.app.ui.activity.video;

import android.Manifest;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.ImageFormat;
import android.graphics.Rect;
import android.graphics.SurfaceTexture;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CameraMetadata;
import android.hardware.camera2.CaptureRequest;
import android.hardware.camera2.CaptureResult;
import android.hardware.camera2.TotalCaptureResult;
import android.hardware.camera2.params.Face;
import android.hardware.camera2.params.MeteringRectangle;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.media.Image;
import android.media.ImageReader;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.util.Size;
import android.view.MotionEvent;
import android.view.Surface;

import com.app.utiles.other.DLog;
import com.app.utiles.other.ToastUtile;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

/**
 * 竖屏，后置摄像头
 * android5.0用这个 指定位置聚焦有问题
 * 视频录制
 * Created by 郭敏 on 2018/1/31 0031.
 */

public class VideoActionActivity3 extends BaseVideoActionActivity {
    private CameraDevice cameraDevice;
    private CaptureRequest.Builder previewBuilder;
    private CaptureRequest previewRequest;
    private CameraCaptureSession cameraSession;
    private CameraCharacteristics characteristics;

    //------------录制时缩放----------
    //手指按下的点为(x1, y1)手指离开屏幕的点为(x2, y2)
    float fingerSpacing;
    int zoomLevel = 0;
    Rect zoom;
    //
    private Size mCaptureSize;//拍照Size
    private ImageReader imageReader;
    //true 开启灯光
    private boolean isLight;

    @Override
    protected void onResume() {
        super.onResume();
        if (zoom != null) {
            zoom.setEmpty();
            zoomLevel = 0;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            // openCamera();
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onPause() {
        super.onPause();
        release();
    }

    //释放资源
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void release() {
        isLight = false;
        closeCamera();
        super.release();
    }

    //切换摄像头
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void cameraSwitchover() {
        isCameraFront = !isCameraFront;
        release();
        openCamera();
    }

    //开启关闭摄像头灯光
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void cameraLight() {
        if (isCameraFront) {
            ToastUtile.showToast("前置摄像头没有灯光");
            return;
        }
        isLight = !isLight;
        if (isLight) {
            previewBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_TORCH);
        } else {
            previewBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF);
        }
        try {
            cameraSession.setRepeatingRequest(previewBuilder.build(), null, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //===========================拍照================================================
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    protected void picture() {
        if (null == cameraDevice || !textureview.isAvailable()) {
            return;
        }
        try {
            CaptureRequest.Builder mCaptureBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
            //获取屏幕方向
            int rotation = getWindowManager().getDefaultDisplay().getRotation();
            DLog.e("拍照", "屏幕方向：" + rotation);
            mCaptureBuilder.addTarget(imageReader.getSurface());
            //isCameraFront是自定义的一个boolean值，用来判断是不是前置摄像头，是的话需要旋转180°，不然拍出来的照片会歪了
            if (isCameraFront) {
                mCaptureBuilder.set(CaptureRequest.JPEG_ORIENTATION, 270);
            } else {
                mCaptureBuilder.set(CaptureRequest.JPEG_ORIENTATION, 90);
            }

            //锁定焦点
            mCaptureBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START);

            //判断预览的时候是否两指缩放过,是的话需要保持当前的缩放比例
            mCaptureBuilder.set(CaptureRequest.SCALER_CROP_REGION, zoom);

            CameraCaptureSession.CaptureCallback CaptureCallback = new CameraCaptureSession.CaptureCallback() {
                @Override
                public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
                    //拍完照unLockFocus
                    unLockFocus();
                }
            };
            cameraSession.stopRepeating();
            //咔擦拍照
            cameraSession.capture(mCaptureBuilder.build(), CaptureCallback, null);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void unLockFocus() {
        try {
            // 构建失能AF的请求
            previewBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
            //闪光灯重置为未开启状态
            //previewBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF);
            //继续开启预览
            cameraSession.setRepeatingRequest(previewRequest, null, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //配置ImageReader
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void imageReader() {
        //2代表ImageReader中最多可以获取两帧图像流
        imageReader = ImageReader.newInstance(mCaptureSize.getWidth(), mCaptureSize.getHeight(),
                ImageFormat.JPEG, 2);
        imageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
            @Override
            public void onImageAvailable(ImageReader reader) {
                Image image = reader.acquireNextImage();
                ByteBuffer buffer = image.getPlanes()[0].getBuffer();
                byte[] data = new byte[buffer.remaining()];
                buffer.get(data);
                //
                String picSavePath = getImagePath();
                try {
                    FileOutputStream fos = new FileOutputStream(picSavePath);
                    fos.write(data, 0, data.length);
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                image.close();
            }
        }, null);
    }

    //==============================视频======================================================
    //录制视频时 支持缩放
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void recordingVideoZoom(MotionEvent event) {

        //活动区域宽度和作物区域宽度之比和活动区域高度和作物区域高度之比的最大比率
        float maxZoom = (characteristics.get(CameraCharacteristics.SCALER_AVAILABLE_MAX_DIGITAL_ZOOM)) * 10;
        Rect m = characteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);
        //
        int action = event.getAction();

        //判断当前屏幕的手指数
        if (event.getPointerCount() > 1) {
            //计算两个触摸点的距离
            float currentFingerSpacing = getFingerSpacing(event);

            if (fingerSpacing != 0) {
                if (currentFingerSpacing > fingerSpacing && maxZoom > zoomLevel) {
                    zoomLevel++;

                } else if (currentFingerSpacing < fingerSpacing && zoomLevel > 1) {
                    zoomLevel--;
                }

                int minW = (int) (m.width() / maxZoom);
                int minH = (int) (m.height() / maxZoom);
                int difW = m.width() - minW;
                int difH = m.height() - minH;
                int cropW = difW / 100 * (int) zoomLevel;
                int cropH = difH / 100 * (int) zoomLevel;
                cropW -= cropW & 3;
                cropH -= cropH & 3;
                zoom = new Rect(cropW, cropH, m.width() - cropW, m.height() - cropH);
                previewBuilder.set(CaptureRequest.SCALER_CROP_REGION, zoom);
            }
            fingerSpacing = currentFingerSpacing;
            try {
                cameraSession.setRepeatingRequest(previewBuilder.build(), new CameraCaptureSession.CaptureCallback() {
                            @Override
                            public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
                                super.onCaptureCompleted(session, request, result);
                            }
                        },
                        null);
            } catch (CameraAccessException e) {
                e.printStackTrace();
                DLog.e("缩放失败", e.getMessage());
            }
            return;
        }
        //点击
        if (action == MotionEvent.ACTION_UP) {
            int x = 0, y = 0;
            //single touch logic,可做点击聚焦操作
            x = (int) event.getRawX();
            y = (int) event.getRawY();
            // 先取相对于view上面的坐标
          /*  double x = event.getX(), y = event.getY(), tmp;

            int realPreviewWidth = mPreviewSize.width, realPreviewHeight = mPreviewSize.height;
            if (90 == mDisplayRotate || 270 == mDisplayRotate) {
                realPreviewWidth = mPreviewSize.height;
                realPreviewHeight = mPreviewSize.width;
            }

            // 计算摄像头取出的图像相对于view放大了多少，以及有多少偏移
            double imgScale = 1.0, verticalOffset = 0, horizontalOffset = 0;
            if (realPreviewHeight * viewWidth > realPreviewWidth * viewHeight) {
                imgScale = viewWidth * 1.0 / realPreviewWidth;
                verticalOffset = (realPreviewHeight - viewHeight / imgScale) / 2;
            } else {
                imgScale = viewHeight * 1.0 / realPreviewHeight;
                horizontalOffset = (realPreviewWidth - viewWidth / imgScale) / 2;
            }

            // 将点击的坐标转换为图像上的坐标
            x = x / imgScale + horizontalOffset;
            y = y / imgScale + verticalOffset;
            if (90 == mDisplayRotate) {
                tmp = x; x = y; y = mPreviewSize.height - tmp;
            } else if (270 == mDisplayRotate) {
                tmp = x; x = mPreviewSize.width - y; y = tmp;
            }

            // 计算取到的图像相对于裁剪区域的缩放系数，以及位移
            Rect cropRegion = mPreviewRequest.get(CaptureRequest.SCALER_CROP_REGION);
            if (null == cropRegion) {
                Log.e(TAG, "can't get crop region");
                cropRegion = mActiveArraySize;
            }

            int cropWidth = cropRegion.width(), cropHeight = cropRegion.height();
            if (mPreviewSize.height * cropWidth > mPreviewSize.width * cropHeight) {
                imgScale = cropHeight * 1.0 / mPreviewSize.height;
                verticalOffset = 0;
                horizontalOffset = (cropWidth - imgScale * mPreviewSize.width) / 2;
            } else {
                imgScale = cropWidth * 1.0 / mPreviewSize.width;
                horizontalOffset = 0;
                verticalOffset = (cropHeight - imgScale * mPreviewSize.height) / 2;
            }

            // 将点击区域相对于图像的坐标，转化为相对于成像区域的坐标
            x = x * imgScale + horizontalOffset + cropRegion.left;
            y = y * imgScale + verticalOffset + cropRegion.top;

            double tapAreaRatio = 0.1;
            Rect rect = new Rect();
            rect.left = clamp((int) (x - tapAreaRatio / 2 * cropRegion.width()), 0, cropRegion.width());
            rect.right = clamp((int) (x + tapAreaRatio / 2 * cropRegion.width()), 0, cropRegion.width());
            rect.top = clamp((int) (y - tapAreaRatio / 2 * cropRegion.height()), 0, cropRegion.height());
            rect.bottom = clamp((int) (y + tapAreaRatio / 2 * cropRegion.height()), 0, cropRegion.height());
*/
            DLog.e("点击", "聚焦 x:" + x + " y:" + y);
            Rect rect = new Rect(x - 50, y - 50, x + 50, y + 50);
            cameraFocus(rect);
        }

    }

    //计算两个触摸点的距离
    private float getFingerSpacing(MotionEvent event) {
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
        return (float) Math.sqrt(x * x + y * y);
    }

    //重新显示相机的预览
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void restCameraPreview() {
        //停止播放
        videoStop();
        //开启相机
        openCamera();
    }

    //播放视频
    @Override
    protected void videoPlay() {
        closeCamera();
        super.videoPlay();
    }

    //=============================相机==========================================
    //关闭相机
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private void closeCamera() {
        if (cameraSession != null) {
            cameraSession.close();
            cameraSession = null;
        }

        if (cameraDevice != null) {
            cameraDevice.close();
            cameraDevice = null;
        }

        if (imageReader != null) {
            imageReader.close();
            imageReader = null;
        }
    }

    //开启相机
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void openCamera() {
        CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
        //后摄像头
        String cameraID = "" + CameraCharacteristics.LENS_FACING_FRONT;
        if (isCameraFront) {
            cameraID = "" + CameraCharacteristics.LENS_FACING_BACK;
        }
        //相机监听
        StateCallback stateCallback = new StateCallback();
        //
        try {
            characteristics = cameraManager.getCameraCharacteristics(cameraID);
            StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
            //获取预览画面输出的尺寸，因为我使用TextureView作为预览
            Size[] imageDimension = map.getOutputSizes(SurfaceTexture.class);
            //获取图片输出的尺寸
            Size[] imageSize = map.getOutputSizes(ImageFormat.JPEG);
            Integer sensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
            DLog.e("相机设置", "sensorOrientation:" + sensorOrientation);
            //获取相机支持的最大拍照尺寸
            mCaptureSize = Collections.max(Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)), new Comparator<Size>() {
                @Override
                public int compare(Size lhs, Size rhs) {
                    return Long.signum(lhs.getWidth() * lhs.getHeight() - rhs.getHeight() * rhs.getWidth());
                }
            });
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }

        try {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
            cameraManager.openCamera(cameraID, stateCallback, null);
        } catch (CameraAccessException e) {
            e.printStackTrace();

        }
    }

    //开始预览
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    protected void cameraPreview() {
        try {
            if (cameraSession != null) {
                cameraSession.close();
                cameraSession = null;
            }
            setVideoConfiguration();
            imageReader();
            //创建预览需要的CaptureRequest.Builder
            previewBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
            //设置聚焦模式
            previewBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
            // previewBuilder.set(CaptureRequest.JPEG_ORIENTATION, Surface.ROTATION_180);
            //  previewBuilder.set(CaptureRequest.JPEG_ORIENTATION
            //         , Surface.ROTATION_180);
            //锁定焦点
            //previewBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START);
            //
            SurfaceTexture surfaceTexture = textureview.getSurfaceTexture();
            Surface viewSurface = new Surface(surfaceTexture);
            previewBuilder.addTarget(viewSurface);
            //
            Surface recorderSurface = getMediaRecorder().getSurface();
            previewBuilder.addTarget(recorderSurface);
            //
            Surface imageSurface = imageReader.getSurface();

            // 创建CameraCaptureSession，该对象负责管理处理预览请求和拍照请求
            cameraDevice.createCaptureSession(Arrays.asList(viewSurface, recorderSurface, imageSurface),
                    new CameraCaptureBack(), null);

        } catch (CameraAccessException e) {
            e.printStackTrace();
            DLog.e("相机打开预览失败", e.getMessage());
        }
    }

    //相机聚焦
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void cameraFocus(Rect rect) {
        previewBuilder.set(CaptureRequest.CONTROL_AF_REGIONS,
                new MeteringRectangle[]{new MeteringRectangle(rect, MeteringRectangle.METERING_WEIGHT_MAX)});
        previewBuilder.set(CaptureRequest.CONTROL_AE_REGIONS,
                new MeteringRectangle[]{new MeteringRectangle(rect, MeteringRectangle.METERING_WEIGHT_MAX)});
        //
        previewBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_AUTO);
        previewBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START);
        // previewBuilder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER, CameraMetadata.CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL);
        //
        //锁定焦点
        //previewBuilder.set(CaptureRequest.CONTROL_AF_REGIONS, new MeteringRectangle[]{new MeteringRectangle(rect, 1000)});
        //previewBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_AUTO);
        //previewBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AE_PRECAPTURE_TRIGGER_IDLE);
        //previewBuilder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER, CameraMetadata.CONTROL_AE_PRECAPTURE_TRIGGER_IDLE);
        //
        // previewBuilder.set(CaptureRequest.CONTROL_AF_REGIONS, new MeteringRectangle[]{new MeteringRectangle(rect, 1000)});
        // previewBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_AUTO);
        //previewBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START);
        //previewBuilder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER, CameraMetadata.CONTROL_AE_PRECAPTURE_TRIGGER_START);
        //
        try {
            cameraSession.setRepeatingRequest(previewBuilder.build(), new FocusCallback(), null);
        } catch (CameraAccessException e) {
            DLog.e("相机聚焦失败", e.getMessage());
        }
    }

    //--------------------------相机相关监听开始-----------------------------
    //配置相机预览
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    class CameraCaptureBack extends CameraCaptureSession.StateCallback {


        @Override
        public void onConfigured(@NonNull CameraCaptureSession session) {
            if (cameraDevice == null) {
                return;
            }
            // 当摄像头已经准备好时，开始显示预览
            cameraSession = session;
            CameraCaptureSession.CaptureCallback CaptureCallback = new CameraCaptureSession.CaptureCallback() {
                @Override
                public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
                    previewBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AE_PRECAPTURE_TRIGGER_IDLE);
                    previewBuilder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER, CameraMetadata.CONTROL_AE_PRECAPTURE_TRIGGER_IDLE);
                    DLog.e("聚焦回调", "-----------------");

                    try {
                        cameraSession.setRepeatingRequest(previewBuilder.build(), null, null);
                    } catch (CameraAccessException e) {
                        DLog.e("聚焦回调错误", e.getMessage());
                    }

                }
            };
            try {
                // 自动对焦
                // previewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
                // 打开闪光灯
                //previewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
                // 显示预览
                previewRequest = previewBuilder.build();
                cameraSession.setRepeatingRequest(previewRequest, CaptureCallback, null);
            } catch (CameraAccessException e) {
                e.printStackTrace();
            }
            DLog.e("相机配置", "onConfigureFailed");
        }

        @Override
        public void onConfigureFailed(@NonNull CameraCaptureSession session) {
            DLog.e("相机配置失败", "onConfigureFailed");
        }
    }

    //相机回调
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    class StateCallback extends CameraDevice.StateCallback {

        @Override
        public void onOpened(@NonNull CameraDevice camera) {
            //打开摄像头
            DLog.e("打开摄像头成功", "开始预览");
            cameraDevice = camera;
            cameraPreview();
        }

        @Override
        public void onDisconnected(@NonNull CameraDevice camera) {
            //关闭摄像头
            if (camera == null) {
                return;
            }
            camera.close();
            cameraDevice = null;
            DLog.e("相机关闭摄像头", "---------");
        }

        @Override
        public void onError(@NonNull CameraDevice camera, int error) {
            //发生错误
            //相机发生错误时调用此方法
            onDisconnected(camera);
            DLog.e("相机发生错误", "error：" + error);
        }
    }

    //聚焦监听
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    class FocusCallback extends CameraCaptureSession.CaptureCallback {
        @Override
        public void onCaptureProgressed(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request,
                                        @NonNull CaptureResult partialResult) {
            process(partialResult);
        }

        @Override
        public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request,
                                       @NonNull TotalCaptureResult result) {
            process(result);
        }

        private void process(CaptureResult result) {
            DLog.e("聚焦回调", "-----------------");
            Integer afState = result.get(CaptureResult.CONTROL_AF_STATE);
            if (null == afState) {
                return;
            }
            //CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED == afState
            //CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED== afState
            if (CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED == afState || afState == CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED) {
                previewBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_IDLE);
                previewBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_VIDEO);
                previewBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
                try {
                    cameraSession.setRepeatingRequest(previewBuilder.build(), null, null);
                } catch (CameraAccessException e) {
                    DLog.e("聚焦", "setRepeatingRequest failed, errMsg: " + e.getMessage());
                }
            }
        }

        //人脸聚焦？
        private void process2(CaptureResult result) {
            Face[] cameraFaces = result.get(CaptureResult.STATISTICS_FACES);
            if (cameraFaces == null) {
                return;
            }
            if (cameraFaces.length == 0) {
                return;
            }
            Rect faceRect = new Rect(cameraFaces[0].getBounds().left, cameraFaces[0].getBounds().top,
                    cameraFaces[0].getBounds().right, cameraFaces[0].getBounds().bottom);
            System.out.println("lammy  faces" + cameraFaces.length);
        }
    }
//------------=---------------相机相关结束-----------------------
}
